package com.wyy.ems_vue.controller;/*
 **
 * @author yangyang
 *@date 2022/07/02 22:55
 **/

import com.wyy.ems_vue.entity.Emp;
import com.wyy.ems_vue.service.EmpService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("emp")
@CrossOrigin
public class EmpController {
    @Autowired
    private EmpService empService;
    @Value("${photo.dir}")
    private  String realPath;

    @GetMapping("findAll")
    public List<Emp> findAll(){
        List<Emp> list = empService.findAll();
        return  list;
    }

    @PostMapping("save")
    public Map<String,Object> save( Emp emp, MultipartFile photo) throws IOException {
        Map<String,Object> map = new HashMap<>();
         try {
             String newFileName= UUID.randomUUID().toString()+"."+FilenameUtils.getExtension(photo.getOriginalFilename());
             photo.transferTo(new File(realPath,newFileName));
             emp.setPath(newFileName);
             empService.save(emp);
             map.put("state", true);
             map.put("msg", "员工信息保存成功");
         }catch (Exception e){
            e.getStackTrace();
             map.put("state", false);
             map.put("msg", "员工信息保存失败");
         }

        return  map;
    }

    @GetMapping("delete")
    public  Map<String,Object> delete(String id){
        Map<String,Object> map=new HashMap<>();
        try {
            Emp one = empService.findOne(id);
            File file = new File(realPath, one.getPath());
            if (file.exists())file.delete();
            empService.delete(id);
            map.put("state", true);
            map.put("msg", "删除员工成功");
        }catch (Exception e){
            map.put("state", false);
            map.put("msg", "删除员工失败");
        }
        return  map;
    }

    @GetMapping("findOne")
    public  Emp findOne(String id){
        Emp one = empService.findOne(id);
        return  one;
    }


    @PostMapping("update")
    public Map<String,Object> update( Emp emp, MultipartFile photo) throws IOException {
        Map<String,Object> map = new HashMap<>();
        try {
            String newFileName= UUID.randomUUID().toString()+"."+FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath,newFileName));
            emp.setPath(newFileName);
            empService.update(emp);
            map.put("state", true);
            map.put("msg", "员工信息保存成功");
        }catch (Exception e){
            e.getStackTrace();
            map.put("state", false);
            map.put("msg", "员工信息保存失败");
        }

        return  map;
    }



}
